﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;


namespace PDAMain.Forms.CustomerWork
{
    public partial class CustomerComplexSel : Form
    {
        //起止日期
        string strFromDate = string.Empty;
        string strEndDate = string.Empty;
        public CustomerComplexSel()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //客服工單號
            string customerID = string.Empty;
            //聯繫人
            string strContactName = string.Empty;
            //聯繫人電話
            string strPhone = string.Empty;
            //問題描述
            string strDescription = string.Empty;
            

            customerID = txtWorkID.Text.ToString();
            strContactName = txtContactName.Text.ToString();
            strPhone = txtContactPhone.Text.ToString();
            strDescription = txtDescription.Text.ToString();
            /*
            strFromDate = dtpFrom.Value.ToString("yyyy/MM/dd hh:mm:ss");
            strEndDate = dtpTo.Value.ToString("yyyy/MM/dd hh:mm:ss");
            */
            
            
          
            //對數據進行驗證
            if (!DataHelper.CustomerWorkDao.verifyData(strPhone, "int"))
            {
                MessageBox.Show("联系人电话输入有误");
                return;
            }
            if (!DataHelper.CustomerWorkDao.verifyData(strFromDate, "date"))
            {
                MessageBox.Show("开始日期输入有误");
                return;
            }
            if (!DataHelper.CustomerWorkDao.verifyData(strEndDate, "date"))
            {
                MessageBox.Show("结束日期输入有误");
                return;
            }

            //獲取數據
            DataTable dt = new DataTable();
            DataSet ds = null;
            try
            {
                ds = DataHelper.CustomerWorkDao.complexSel(customerID, strContactName, strPhone, strDescription, strFromDate, strEndDate);
            }
            catch
            {
                ds = DataHelper.CustomerWorkDao.CustomerComplexSel(customerID, strContactName, strPhone, strDescription);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["UPDATEDT"]) < Convert.ToDateTime(strFromDate) || Convert.ToDateTime(ds.Tables[0].Rows[index]["UPDATEDT"]) > Convert.ToDateTime(strEndDate))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                QueryGrid.DataSource = null;
                QueryGrid.TableStyles.Clear();
                QueryGrid.Refresh();
                MessageBox.Show("无数据");
                return;
            }

            dt = ds.Tables[0];
            for (int k = 0; k < dt.Rows.Count; k++)
            {

                if (dt.Rows[k]["workStatus"].ToString() == "1")
                {
                    dt.Rows[k]["workStatus"] = "处理中";
                }
                else if (dt.Rows[k]["workStatus"].ToString() == "2")
                {
                    dt.Rows[k]["workStatus"] = "转单";
                }
                else if (dt.Rows[k]["workStatus"].ToString() == "3")
                {
                    dt.Rows[k]["workStatus"] = "报修";
                }
                else if (dt.Rows[k]["workStatus"].ToString() == "9")
                {
                    dt.Rows[k]["workStatus"] = "已处理";
                }
                else
                {
                    dt.Rows[k]["workStatus"] = "未定义状态";
                }
            }
            //綁定數據
            QueryGrid.DataSource = dt;
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = dt.TableName;

            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
            col1.HeaderText = "活动编号";
            col1.MappingName = "activityUID";
            col1.Width = 83;
            dts.GridColumnStyles.Add(col1);

            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
            col2.HeaderText = "地址";
            col2.MappingName = "houseLocation";
            col2.Width = 110;
            dts.GridColumnStyles.Add(col2);

            DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
            col3.HeaderText = "优先级";
            col3.MappingName = "workPriority";
            col3.Width = 60;
            dts.GridColumnStyles.Add(col3);

            DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
            col4.HeaderText = "状态";
            col4.MappingName = "workStatus";
            col4.Width = 60;
            dts.GridColumnStyles.Add(col4);

            QueryGrid.TableStyles.Clear();
            QueryGrid.TableStyles.Add(dts);
            QueryGrid.Refresh();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            CustomerList list = new CustomerList();
            list.Show();
            this.Close();
        }

        private void dtpFrom_ValueChanged(object sender, EventArgs e)
        {
            if (dtpFrom.Value != null)
            {
                strFromDate = dtpFrom.Value.ToString("yyyy/MM/dd hh:mm:ss");
                dtpTo.Value = dtpFrom.Value.AddDays(7);
                strEndDate = dtpTo.Value.ToString("yyyy/MM/dd hh:mm:ss");
            }
        }

        private void dtpTo_ValueChanged(object sender, EventArgs e)
        {
            if (dtpTo.Value != null )
            {
                strEndDate = dtpTo.Value.ToString("yyyy/MM/dd hh:mm:ss");
                dtpFrom.Value = dtpTo.Value.AddDays(-7);
                strFromDate = dtpFrom.Value.ToString("yyyy/MM/dd hh:mm:ss");
            }
        }
    }
}